package grsu.epam.airline.action;

import java.util.HashMap;
import java.util.Map;

import grsu.epam.airline.form.CrewForm;
import grsu.epam.airline.service.FlightCrewService;
import grsu.epam.airline.service.SpecialityService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;

public class FlightCrewModifyAction extends LookupDispatchAction {
	String target = null;

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			SpecialityService specialityService = new SpecialityService();
			request.setAttribute("specialities",
					specialityService.getAllSpeciality());
			CrewForm input = (CrewForm) form;
			request.setAttribute("flightCrewId", input.getFlightCrewId());
			target = ForwardActionTo.CHOOSEEMPLOYEEFLIGHTCREW;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);

	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			CrewForm input = (CrewForm) form;
			FlightCrewService flightCrewService = new FlightCrewService();
			flightCrewService.updateFlightCrew(0, input.getEmployeeId());
			request.setAttribute("flightCrew", flightCrewService
					.getFlightCrewById(input.getFlightCrewId()));
			target = ForwardActionTo.UPDATEFLIGHTCREW;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);

	}

	@Override
	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("flightCrew.employee.add", "add");
		map.put("flightCrew.employee.delete", "delete");
		return map;
	}

}
